package com.tjsinfo.tjsoa.messageImportant.bo;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.JDBCDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.messageImportant.dao.MessageImportantInceptDAO;
import com.tjsinfo.tjsoa.messageImportant.vo.MessageImportantIncept;
import com.tjsinfo.tjsoa.messageImportant.vo.MessageImportantMain;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;


public class MessageImportantInceptBo implements IMessageImportantInceptBo {
	private IQbcDao qbcDao;
private MessageImportantInceptDAO inceptDAO;
private JDBCDao jdbcdao;


	public MessageImportantInceptDAO getInceptDAO() {
	return inceptDAO;
}

public void setInceptDAO(MessageImportantInceptDAO inceptDAO) {
	this.inceptDAO = inceptDAO;
}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.MessageImportant.MessageImportant_Incept.IMessageImportantInceptBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.MessageImportant.MessageImportant_Incept.IMessageImportantInceptBo#findByReceiveUser(com.tjsinfo.tjsoa.system.TjsUser.TjsUser)
	 */
	public List findByReceiveUser(Integer userid,String sql,String orderColumn,String order) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MessageImportantIncept.class);
		DetachedCriteria criteria1 = criteria.createCriteria("messageImportantMain");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			criteria1.add(Restrictions.le("sendtime", sdf.parse(sdf.format(new Date()))));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		criteria.add(Restrictions.eq("receiveUserId", userid));
		criteria.add(Restrictions.eq("sendSql", sql));
		System.out.println(orderColumn);
		if (orderColumn!=null && !"".equals(orderColumn)) {
			if ("fasong".equals(orderColumn)) {
				if ("asc".equals(order)) {
					criteria1.addOrder(Order.asc("datetime"));
				}else {
					criteria1.addOrder(Order.desc("datetime"));
				}
			}else {
				if ("asc".equals(order)) {
					criteria.addOrder(Order.asc(orderColumn));
				}else {
					criteria.addOrder(Order.desc(orderColumn));
				}
			}
		}else {
			criteria.addOrder(Order.asc("state"));
			criteria.addOrder(Order.desc("id"));
		}
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	public String  save(MessageImportantIncept incept) {
		String i=inceptDAO.save(incept);
			return i;
	}
	//修改查阅状态

	public String update(String i,String username) {
		List<MessageImportantIncept> list = inceptDAO.findByProperty("id", i);
		
		MessageImportantMain main = new MessageImportantMain();
		Date date=new Date();
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dates = dateFormat.format(date);
		if (list.size()>0) {
			main = list.iterator().next().getMessageImportantMain();
			
			String nowsql = "";
			Configuration configuration = null;
			try {
				configuration = new PropertiesConfiguration("com/tjsinfo/tjsoa/mail/properties/systemResource.properties");
				nowsql = configuration.getString("nowSQL");
			} catch (ConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			for (int j = 1; j <= 5; j++) {
				String sqlName = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty(Integer.valueOf(j).toString());
				
				if (sqlName!=null && !"".equals(sqlName)) {
					String sql = "update "+sqlName+".dbo.t_messageImportant_incept set datetime=convert(datetime,'"+dates+"'),state='1'  where main_id='"+main.getId()+"' and receive_username='"+username+"' and send_sql='"+nowsql+"'";
					jdbcdao.executeUpdate(sql);
					jdbcdao.closeStmt();
					jdbcdao.closeConn();
				}
			}
		}
		
		return dates;
	}
	//删除发送内容


public void delete(String i) {
	String sql="delete dbo.t_messageImportant_incept wbere where id='"+i+"'";
	jdbcdao.executeUpdate(sql);
	jdbcdao.closeStmt();
	jdbcdao.closeConn();
}
	public JDBCDao getJdbcdao() {
		return jdbcdao;
	}

	public void setJdbcdao(JDBCDao jdbcdao) {
		this.jdbcdao = jdbcdao;
	}
	
	public String getDai(Integer userid,String username) {
		String nowSQL = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("nowSQL");
		
		DetachedCriteria criteria = DetachedCriteria.forClass(MessageImportantIncept.class);
		DetachedCriteria criteria1 = criteria.createCriteria("messageImportantMain");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			criteria1.add(Restrictions.le("sendtime", sdf.parse(sdf.format(new Date()))));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		criteria.add(Restrictions.and(Restrictions.or(Restrictions.eq("receiveUserId", userid), Restrictions.eq("receiveUsername", username)), Restrictions.eq("state", 0)));
		criteria.add(Restrictions.eq("sendSql", nowSQL));
		criteria.addOrder(Order.asc("datetime"));
		List list = qbcDao.getCriteria(criteria);
		String str = "";
		if (list.size()>0) {
			MessageImportantIncept incept = (MessageImportantIncept) list.iterator().next();
			str = incept.getMessageImportantMain().getSendUsername()+":"+incept.getMessageImportantMain().getInfo();
		}
		return str;
	}
}
